#!/bin/bash

#-------------------------------------------------------------------------------
# Installation return codes are:
#
# 0 - no error
# 10 - usage error
# 20 - invalid/non-existent directory on HMC
# 30 - install file not found
# 40 - RPM test verify install error
# 50 - RPM install error
# 60 - 'tar' command failure
#
# Except for call rpm command to query, all calls to rpm must go
# go through dorpm, eraserpm etc..
#
# *The actual value returned will be 1x, 2x, etc, where x will be either 0 or 4
# which is based on any RPM un-install errors. Else, '0' if successful completion
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Initialize script return code, log file name
#-------------------------------------------------------------------------------
exitRC=0
logFile=/tmp/hmc_install.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {
    # keep the log file, but ensure a different user can overwrite it next time
    chmod 666 $logFile

    if [ $1 -ne 0 ] ; then
        exit $1$exitRC
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ] ; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS` ; do
            if [ "$r" = "$i" ] ; then
                return
            fi
        done
    fi
     
    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    rpm $*
    RC=$?
    if [ $RC -ne 0 ] ; then
        echo "rpm fileset, $2, installation error, $RC" >> $logFile
    fi
    return $RC
}

#-------------------------------------------------------------------------------
# RPM erase rpm
#-------------------------------------------------------------------------------
eraserpm() {
    # If rpm is part of no update list then do not do anything with it
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ] ; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS` ; do
            if [ "$x" = "$i" ] ; then
                return
            fi
        done
    fi
    rpm -v $*
    if [ $? -ne 0 ] ; then
        exitRC=4
        echo "error removing rpm fileset named $2" >> $logFile
    fi
}

#-------------------------------------------------------------------------------
# RPM test install
#-------------------------------------------------------------------------------
testrpm() {
    rpm --test  $1 $2 $3 $4
    return $?
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" = "" ] ; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    exit_cleanup 1
fi

# check if directory exists
if [ ! -d $image ] ; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    exit_cleanup 2
fi

VER=`hsc version | grep Version | cut -f2 -d ":" | awk '{print $1}'`
REL=`hsc version | grep Release | cut -f2 -d ":" | awk '{print $1}'`

if [ $VER != "3" -o $REL != "3.2" ]; then
    echo "Incompatible Fix. Fix not installed"
    exit_cleanup 3
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ] ; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ] ; then
    ln -s /opt/IBMJava2-141 /opt/IBMJava
fi

rpm -q rsct.service 1>&2 2>/dev/null
if [ $? -eq 0 ] ; then
    dorpm -Uvh $image/rmc/rsct.service-*rpm --force --nodeps 2>> $logFile
else
    dorpm -ivh $image/rmc/rsct.service-*rpm --force --nodeps 2>> $logFile
fi

cd /usr/websm/codebase/pluginjars
mv hsc.jar hsc.jar.orig
cp $image/hsc.jar .

grep -v "U800514 : Corrective Service" /opt/hsc/data/version > /tmp/version
echo "U800514 : Corrective Service" >> /tmp/version
mv /tmp/version /opt/hsc/data/version

exit_cleanup 0
